package apigateway.project.core;

import apigateway.project.commen.config.Rule;
import apigateway.project.commen.config.ServiceDefinition;
import apigateway.project.commen.config.ServiceInstance;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

public class CacheConfig {
    //key为规则的id，value为对应的规则
    private ConcurrentHashMap<String, Rule> ruleMap = new ConcurrentHashMap<>();


    //key为后端服务的id
    private ConcurrentHashMap<String,List<Rule>> serverRuleMap = new ConcurrentHashMap<>();


    //服务定义集合,使用ConcurrentHashMap保证线程安全
    private ConcurrentHashMap<String, ServiceDefinition> serviceDefinitionMap = new ConcurrentHashMap<>();

    //服务对应的实例集合
    private ConcurrentHashMap<String, Set<ServiceInstance>> serviceInstancesMap = new ConcurrentHashMap<>();

    private static final CacheConfig cacheConfig = new CacheConfig();

    private CacheConfig() {}

    public static CacheConfig getCacheConfig() {
        return cacheConfig;
    }
    //熔断规则缓存
    CopyOnWriteArrayList<DegradeRule> degradeRules = new CopyOnWriteArrayList<>();
    /**
     * 每当配置发生更新时，由于不好确定具体哪里发生了变化，所以索性全都重新缓存一遍
     * @param rules
     */
    public void cache(List<Rule> rules) {
        ConcurrentHashMap<String, Rule> newRuleMap = new ConcurrentHashMap<>();
        ConcurrentHashMap<String, List<Rule>> newServerRuleMap= new ConcurrentHashMap<>();
        for (Rule rule : rules) {
            newRuleMap.put(rule.getId(), rule);
            List<Rule> rules1 = newServerRuleMap.get(rule.getServiceName());
            if (rules1 == null) {
                rules1 = new ArrayList<>();
            }
            rules1.add(rule);
            newServerRuleMap.put(rule.getServiceName(), rules1);
        }
        ruleMap = newRuleMap;
        serverRuleMap = newServerRuleMap;
    }

    public Rule getRuleByRuleId(String ruleId) {
        return ruleMap.get(ruleId);
    }

    public List<Rule> getRulesByServiceId(String ServiceId) {
        return serverRuleMap.get(ServiceId);
    }

    public void putDegradeRule(DegradeRule degradeRule) {
        degradeRules.add(degradeRule);
    }
    public List<DegradeRule> getDegradeRules(String sourceName) {
        return degradeRules;
    }

    public List<Rule> getAllRules() {
        return ruleMap.values().stream().toList();
    }
//    public Rule getRuleByPredicate(GlobalRequest request) {
//        ruleMap.forEach((ruleId, rule) -> {
//            Map<String, String> predicates = rule.getPredicates();
//            if(predicates != null) {
//                for (String predicate : predicates.keySet()) {
//
//            }
//            }
//
//        });
//    }

    public void putServiceDefinition(String serviceName, ServiceDefinition serviceDefinition) {
        serviceDefinitionMap.put(serviceName, serviceDefinition);
    }

    public void putServiceInstances(String serviceName, Set<ServiceInstance> serviceInstances) {
        serviceInstancesMap.put(serviceName, serviceInstances);
    }

//    public List<Rule> getRules(){
//        return rules;
//    }

    //获取后端服务实例列表
    public Set<ServiceInstance> getServiceByServiceName(String serviceName){
        return serviceInstancesMap.get(serviceName);
    }
}
